<template>
	<!-- 20230426 -->
	<view class="mall-list-page">

		<!-- 页面内容 -->
		<view class="page-content padding-bottom-safe-20">
			<!-- 搜索和筛选 -->
			<view class="jc-header">
				<!-- 搜索 -->
				<view class="search_block padding-lr-30 bg-main">

					<!-- #ifdef APP-PLUS -->
					<!-- 占位 view，避免页面内容出现在状态栏 -->
					<view class="height-status-bar"></view>
					<!-- #endif -->

					<!-- 搜索 -->
					<view class="flex justify-between align-center">
						<!-- #ifdef APP-PLUS -->
						<!-- 返回图标 -->
						<text class="tyIcon-arrow-left margin-right-15 text-44 text-ff" @click="back"></text>
						<!-- #endif -->

						<!-- 搜索框 -->
						<view class="width-100p">
							<ty-bar-search :value.sync="keyword" bg-color="#FFFFFF" @search="search"></ty-bar-search>
						</view>

						<!-- 切换列表样式图标 -->
						<text class="margin-left-15 text-44 text-ff"
							:class="type == 1 ? 'tyIcon-list-row' : 'tyIcon-list-square'"
							@click="type == 1 ? type = 2 : type = 1"></text>
					</view>
				</view>

				<!-- 筛选 -->
				<view class="line-height-header bg-ff">
					<view class="width-100p flex justify-around">
						<!-- 新品 -->
						<view class="sort-item" :class="{'active' : order == 1}" @tap="screen(1)">
							<view class="text">新品</view>
							<view class="bottom-line"></view>
						</view>
						<!-- 销量 -->
						<view class="sort-item" :class="{'active' : order == 2}" @tap="screen(2)">
							<view class="text">销量</view>
							<view class="bottom-line"></view>
						</view>
						<!-- 价格 -->
						<view class="flex align-center" @tap="screen(3)">
							<view class="sort-item" :class="{'active' : order == 3 || order == 4}">
								<view class="text">价格</view>
								<view class="bottom-line"></view>
							</view>
							<view class="margin-left-15 flex flex-direction align-center">
								<view class="triangle" :class="{'active': order == 3}"></view>
								<view class="triangle margin-top-5" :class="{'active': order == 4}"
									style="margin-top: 2rpx; transform: rotate(180deg);"></view>
							</view>
						</view>

						<!-- 筛选 -->
						<view @tap="isScreenLayerShow = !isScreenLayerShow">
							<text class="text-28 text-33">筛选</text>
							<text class="margin-left-15 tyIcon-filter text-24 text-77"></text>
						</view>
					</view>

				</view>

			</view>

			<!-- 列表 -->
			<view class="margin-top-status-bar-header2">
				<jc-mall-productList :list="list" :loadStatus="loadStatus"
					:type="type"></jc-mall-productList>
			</view>
		</view>

		<!-- 页面浮层 -->
		<view class="page-layer">
			<!-- 筛选浮层 -->
			<ty-layer-pop-up v-if="isScreenLayerShow">
				<!-- 浮层内容 -->
				<view class="layer-content padding-lr-40 padding-top-40 bg-ff" @click.stop="">
					<view class="text-40 text-bold">筛选</view>
					<!-- 价格 -->
					<view class="margin-top-40">
						<text class="text-28 text-bold text-33">价格区间（元）</text>
						<view class="margin-top-20 flex align-center">
							<!-- 最低价格 -->
							<input class="width-250 line-height-70 radius-35 bg-f5 text-center text-26" type="number"
								placeholder-class="text-26 text-aa" v-model="price_min" placeholder="最低价" />

							<!-- 价格中间横线 -->
							<text class="border-line"></text>

							<!-- 最高价格 -->
							<input class="width-250 line-height-70 radius-35 bg-f5 text-center text-26" type="number"
								placeholder-class="text-26 text-aa" v-model="price_max" placeholder="最高价" />
						</view>
					</view>

					<!-- 按钮 -->
					<view class="layer-btns flex">
						<view class="layer-btn width-290 line-height-80 bg-auxiliary text-center text-28 text-ff"
							@tap="reset">重置</view>
						<view
							class="layer-btn width-290 line-height-80 bg-main line-height-80 text-center text-28 text-ff"
							@tap="sure">确定</view>
					</view>
				</view>
			</ty-layer-pop-up>
		</view>
	</view>
</template>

<script>
	import mall from './api/mall.js'
	export default {
		data() {
			return {
				first_category_id: '',
				second_category_id: '',
				// 默认搜索框文字
				keyword: '',
				order: 1,
				// 加载状态
				loadStatus: '',
				// 商品列表
				list: [],
				//列表类型 1--一行一个 2--一行两个
				type: 2,
				// 筛选浮层是否显示
				isScreenLayerShow: false, 
				price_min: '',
				price_max: '',

				pull_down_refresh: false //下拉刷新状态 true--进行中 false--没有在进行中
			}
		},

		onLoad(options) {
			this.first_category_id = options.first_category_id || 0
			this.second_category_id = options.second_category_id || 0
			this.keyword = options.keyword ? decodeURI(options.keyword) : ''
		},

		onReady() {
			this.loadList(1);
		},

		onReachBottom() {
			this.loadList(0);
		},

		/**
		 * 监听用户下拉动作，一般用于下拉刷新
		 * 2022-05-23
		 */
		onPullDownRefresh() {
			this.pull_down_refresh = true
			//加载场次产品列表
			this.loadList(1)
		},

		methods: {
			/**
			 * 获取商品列表
			 * @param {int} isFirstPage 是否第一页 0--不是 1--是
			 * @date 2022-10-27
			 */
			loadList(isFirstPage) {
				mall.listProduct(this, isFirstPage, this.first_category_id, this.second_category_id, this.keyword, '', this
					.price_min, this.price_max, this.order).then(res => {
					if (this.pull_down_refresh) {
						this.can_pull_down = false
						uni.stopPullDownRefresh()
					}
				})
			},

			// 切换筛选条件
			screen(order) {
				if (order == 3) {
					this.order = this.order != 3 ? 3 : 4
				} else {
					this.order = order
				}
				this.loadList(1);
			},

			//搜索
			search(e) {
				this.keyword = e.keyword
				this.loadList(1);
			},

			// 重置
			reset() {
				this.price_min = ''
				this.price_max = ''
			},

			// 确定
			sure() {
				this.isScreenLayerShow = false
				this.loadList(1);
			},

			//返回
			back() {
				uni.navigateBack({
					delta: 1
				})
			}
		}
	}
</script>

<style scoped>
	/* 筛选栏 */
	.sort-item {
		position: relative;
	}

	.sort-item .text {
		font-size: 28rpx;
		color: #333;
	}

	.sort-item.active .text {
		color: #1a1a1a;
		font-weight: bold;
	}

	.sort-item .bottom-line {
		position: absolute;
		bottom: 20rpx;
		left: calc(50% - 20rpx);
		width: 40rpx;
		height: 4rpx;
		background: #fff;
	}

	.sort-item.active .bottom-line {
		background: var(--main);
	}

	/* 三角形 */
	.triangle {
		border-left: 6rpx solid transparent;
		border-right: 6rpx solid transparent;
		border-bottom: 8rpx solid #ccc;
		width: 0;
		height: 0;
	}

	/* 三角形选中样式 */
	.triangle.active {
		border-bottom: 8rpx solid var(--main);
	}

	/* 筛选 */
	.layer-content {
		position: absolute;
		right: 0;
		width: 650rpx;
		height: 100vh;
	}

	/* 价钱中间横线 */
	.border-line {
		margin: 0 20rpx;
		width: 30rpx;
		height: 2rpx;
		background: #AAA;
	}

	/* 浮层按钮 */
	.layer-btns {
		position: absolute;
		bottom: 20rpx;
	}

	.layer-btn:first-child {
		border-radius: 40rpx 0 0 40rpx;
	}

	.layer-btn:last-child {
		border-radius: 0 40rpx 40rpx 0;
	}
</style>